package com.iteima.reggie.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iteima.reggie.common.CustomException;
import com.iteima.reggie.entity.Category;
import com.iteima.reggie.entity.Dish;
import com.iteima.reggie.entity.Setmeal;
import com.iteima.reggie.mapper.CategoryMapper;
import com.iteima.reggie.service.CategoryService;
import com.iteima.reggie.service.DishService;
import com.iteima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService {

    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    @Override
    public void remove(Long ids) {
        log.info("ids={}",ids);
        //查询套餐中是否包含该分类
        LambdaQueryWrapper<Setmeal> setmeaQueryWrapper = new LambdaQueryWrapper<>();
        setmeaQueryWrapper.eq(Setmeal::getCategoryId,ids);
        int count1 = setmealService.count(setmeaQueryWrapper);
        if (count1 > 0){
            //套餐关联了该分类
            throw  new CustomException("当前分类关联了一个套餐,不能删除");
        }
        //检查询菜品中是否包含该分类
        LambdaQueryWrapper<Dish> dishQueryWrapper = new LambdaQueryWrapper<>();
        dishQueryWrapper.eq(Dish::getCategoryId,ids);
        int count2 = dishService.count(dishQueryWrapper);
        if (count2 > 0 ){
            //菜品关联了该分类
            throw  new CustomException("当前分类关联了一个菜品,不能删除");
        }

        super.removeById(ids);
    }
}




